package net.neiquan.applibrary.numberpicker.view;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;


import net.neiquan.applibrary.R;

import java.util.ArrayList;

public class AlertPickerSingle extends Dialog {

    protected AlertPickerSingle(Context context) {
        super(context, R.style.AlertChooser);
        setOwnerActivity((Activity) context);
    }

    protected AlertPickerSingle(Context context, int theme) {
        super(context, theme);
        getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Window window = this.getWindow();
        WindowManager.LayoutParams params = window.getAttributes();
        params.gravity = Gravity.BOTTOM;
        window.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        window.setAttributes(params);
    }

    public static class Builder {
        private Context context;
        private AlertPickerSingle chooser;
        private CharSequence title;
        private ArrayList<String> itemTextList;
        private boolean cancelable = true;
        private boolean canceledOnTouchOutside = true;
        private OnItemSelectListener onItemClickListener;
        private boolean isCall = false;// 保证只调用一次


        public Builder setDefaultIndex(int defaultIndex) {
            this.defaultIndex = defaultIndex;
            return this;
        }

        private int defaultIndex = 0;

        public Builder setOnItemSelectListener(OnItemSelectListener onItemClickListener) {
            this.onItemClickListener = onItemClickListener;
            return this;
        }

        public Builder(Context context) {
            chooser = new AlertPickerSingle(context);
            this.context = context;
            init();
        }

        public Builder(Context context, int theme) {
            chooser = new AlertPickerSingle(context, theme);
            this.context = context;
            init();
        }

        private void init() {
        }

        public Builder setTitle(CharSequence title) {
            this.title = title;
            return this;
        }

        public Builder setTitle(int titleRes) {
            this.title = context.getResources().getString(titleRes);
            return this;
        }

        public Builder setData(ArrayList<String> itemTextList) {
            this.itemTextList = itemTextList;
            return this;
        }

        public Builder setCancelable(boolean cancelable) {
            this.cancelable = cancelable;
            return this;
        }

        public Builder setCanceledOnTouchOutside(boolean canceled) {
            this.canceledOnTouchOutside = canceled;
            return this;
        }

        private AlertPickerSingle create() {
            if (chooser == null)
                return null;
            //View iPanel = LayoutInflater.from(context).inflate(R.layout.dialog_actor_type, null);
            View iPanel = LayoutInflater.from(context).inflate(R.layout.picker_single, null);
            TextView iCancel = (TextView) iPanel.findViewById(R.id.cancel);
            final TextView titleTv = (TextView) iPanel.findViewById(R.id.title_tv);
            TextView confirmTv = (TextView) iPanel.findViewById(R.id.confirm_tv);
            final ScrollerNumberPicker scrollPic = (ScrollerNumberPicker) iPanel.findViewById(R.id.scroll_pic);
            if (title != null) {
                titleTv.setVisibility(View.VISIBLE);
                titleTv.setText(title);
            } else {
                titleTv.setVisibility(View.GONE);
            }

            scrollPic.setData(itemTextList);
            scrollPic.setDefault(defaultIndex);
            scrollPic.setOnSelectListener(new ScrollerNumberPicker.OnSelectListener() {

                @Override
                public void endSelect(int id, String text) {
//					if (onItemClickListener!=null){
//						onItemClickListener.endSelect(chooser,false,id,text);
//					}
                }

                @Override
                public void selecting(int id, String text) {

                }
            });
            confirmTv.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    isCall = true;
                    chooser.dismiss();
                }
            });
            chooser.setCancelable(cancelable);
            chooser.setCanceledOnTouchOutside(canceledOnTouchOutside);
            chooser.setContentView(iPanel);
            chooser.setOnDismissListener(new OnDismissListener() {
                @Override
                public void onDismiss(DialogInterface dialog) {
                    if (onItemClickListener != null) {
                        if (title != null) {
                            if (!isCall)
                                return;
                        }
                        onItemClickListener.endSelect(chooser, false, scrollPic.getSelected(), scrollPic.getSelectedText());
                    }
                }
            });
            return chooser;
        }

        public AlertPickerSingle show() {
            create().show();
            return chooser;
        }
    }

    public interface OnItemSelectListener {
        void endSelect(AlertPickerSingle chooser, boolean isCancel, int id, String text);
    }
}

